package main

import (
	"path/filepath"
	"pkg/config"
	"pkg/models"
	"pkg/sandbox"
	"pkg/utils/logger"

	"github.com/BurntSushi/toml"
	"github.com/gofiber/fiber/v2"
)

var Config config.TomlConfig

func init() {
	if err := logger.InitGlobalLogger(loggerConfig()); err != nil {
		panic(err)
	}
	defer logger.Sync()
	if _, err := toml.DecodeFile("config.toml", &Config); err != nil {
		logger.Debug("解析配置文件失败: ", err)
	}
}
func main() {
	app := fiber.New()
	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})
	app.Post("/judge", func(c *fiber.Ctx) error {
		inputStruct := new(models.InputStruct)
		if err := c.BodyParser(inputStruct); err != nil {
			return c.Status(fiber.StatusBadRequest).SendString("Bad Request")
		}
		logger.Debug("++++++++++++++++++++++++++++++++++++++++")
		logger.Debug(inputStruct)
		sb := sandbox.NewSandbox(inputStruct)
		result, err := sb.Judge()
		if err != nil {
			return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error" + err.Error())
		}
		logger.Debug(result)
		logger.Debug("++++++++++++++++++++++++++++++++++++++++")
		return c.JSON(result)
	})
	app.Listen(":" + Config.App.Port)

}

func loggerConfig() logger.Config {
	cfg := logger.Config{
		Level:      logger.DebugLevel,
		Filename:   filepath.Join("./logs", "app.log"),
		MaxSize:    100, // MB
		MaxBackups: 7,   // 保留7天
		MaxAge:     30,  // 保留30天
		Compress:   true,
		Console:    true,
	}
	return cfg
}
